/* Copyright (C) 
 * 2015 - wxm71 <weixing.mei@aispeech.com>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

#include <stdio.h>
#include <stdarg.h>

#include "wtk_utils_debug.h"

void wtk_utils_log_print(int clr, const char *flag,  const char *file, const char *func, int lineno, char *fmt, ...) {
    char buf[6144] = {0};
    char *ptr = buf;

    int len = 0;

    size_t szLen;

    // get time
    time_t timeNow = time(NULL);
    len = strftime(ptr, 128, "%F %T", localtime(&timeNow));
    ptr += len;

    // color flag
#ifdef WTK_CLR_PRINT
    len = sprintf(ptr, "\e[%d;3m", clr);
    ptr += len;
#endif
    len = sprintf(ptr, " [%5s]\t", flag);
    ptr += len;

    va_list ap;
    va_start(ap, fmt);

    szLen = 5504;
    len = vsnprintf(ptr, szLen, fmt, ap);
    if(len>szLen) {
        ptr += szLen;
    } else{
        ptr += len;
    }

    va_end(ap);

#ifdef WTK_CLR_PRINT
    len = sprintf(ptr, "\e[0m");
    ptr += len;
#endif

    // file, func, line-no
    szLen = 512;
    len = snprintf(ptr, szLen, "\t\t[%s], [%s],[%d]\n", file, func, lineno); 
    if(len>szLen) {
        ptr += szLen;
    } else {
        ptr += len;
    }
    *ptr = 0;

    printf("%s", buf);

    fflush(stdout); 
    fflush(stderr);
}
